//
//  TravelPhotoPageViewController.m
//  旅游咨询
//
//  Created by 徐斌 on 15/4/18.
//  Copyright (c) 2015年 xubin. All rights reserved.
//

#import "TravelPhotoPageViewController.h"
#import "MJRefresh.h"
@interface TravelPhotoPageViewController ()

@end

@implementation TravelPhotoPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"热门图片";
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.tabBarController.tabBar.translucent = NO;
    self.modelArray = [NSMutableArray array];
//    self.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.headerReferenceSize = CGSizeMake(0, 0);
    
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
    
    [self.myCollectionView registerClass:[TravelPhotoPageCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    flowLayout.itemSize = CGSizeMake(self.myCollectionView.frame.size.width / 2 - 15, self.myCollectionView.frame.size.width / 2 - 15);
//    self.myCollectionView.bounces = NO;
    [self.view addSubview:self.myCollectionView];
//    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
//    
//    
//    NSString *url_string = [NSString stringWithFormat:@"http://api.breadtrip.com/featured/photos/?start=0&count=12"];
//    //   http://api.breadtrip.com/featured/photos/?start=0&count=12
//    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
//    
//    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
//    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
//    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        [netWorkManager stopMonitoring];
//        
//        
//        NSMutableDictionary *dic = responseObject;
//        
//        NSMutableArray *array = [dic objectForKey:@"items"];
//        for (NSMutableDictionary *dic2 in array) {
//            TravelPhotoPageModel *model = [[TravelPhotoPageModel alloc]init];
//            model.photo_s = [dic2 objectForKey:@"photo_w640"];
//            model.address = [dic2 objectForKey:@"name"];
//            model.detailId = [dic2 objectForKey:@"trip_id"];
//            
//            
//            
//            
//            [self.modelArray addObject:model];
//            [model release];
//        }
//        [self.myCollectionView reloadData];
//       
//        // 刷新
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        
//        NSLog(@"失败==== %@",error);
//        
//    }];

    
    self.next_cursor = 0;
    
    [self addFooter];
    [self addHeader];
    
    
    // Do any additional setup after loading the view.
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TravelPhotoPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.model = [self.modelArray objectAtIndex:indexPath.row];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TravelNotesDetailViewController *travelVC = [[TravelNotesDetailViewController alloc]init];
    
    travelVC.myId = [[self.modelArray objectAtIndex:indexPath.row] detailId];
    travelVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:travelVC animated:YES];
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.modelArray.count;
    
}


// 下拉刷新

- (void)addHeader
{
    __block TravelPhotoPageViewController *vc = self;
    [self.myCollectionView addHeaderWithCallback:^{
        vc.isUpLoading = NO;
        [vc getAFNData:0];
        NSLog(@" 请求第一页数据");
        
    }];
    [self.myCollectionView headerBeginRefreshing];
    
}

- (void)getAFNData:(NSInteger)nextcursor
{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = [NSString stringWithFormat:@"http://api.breadtrip.com/featured/photos/?start=%ld&count=12", (long)nextcursor];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSMutableDictionary *dica = responseObject;
        
        if (self.isUpLoading == NO) {
            
            [self.modelArray removeAllObjects];
            
        }
        
        
        
        NSMutableArray *array = [dica objectForKey:@"items"];
        for (NSMutableDictionary *dic2 in array) {
            TravelPhotoPageModel *model = [[TravelPhotoPageModel alloc]init];
            model.photo_s = [dic2 objectForKey:@"photo_w640"];
            model.address = [dic2 objectForKey:@"name"];
            model.detailId = [dic2 objectForKey:@"trip_id"];
            
            
            
            
            [self.modelArray addObject:model];
            //            NSLog(@"self %ld",self.array.count);
            [model release];
        }
        
        
        
//        self.next_cursor = [[dic objectForKey:@"next_cursor"] integerValue];
        
        
        //        NSLog(@"aaa= == = === = %@",responseObject);
        
        
        // 让下拉自动刷新停止
        [self.myCollectionView headerEndRefreshing];
        // 刷新tableview
        [self.myCollectionView reloadData];
        // 让上拉自动刷新停止
        [self.myCollectionView footerEndRefreshing];
        
        
        
        // 如果页面是tableview 那么请求数据之后一定要刷新tableview
        // 如果页面是类似详情页面 label uiimageview 等其他控件 那么请求完数据 一定要给label uiimageview等控件重新赋值
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        // 弹出一个提示框提示用户当前网络不给力
        
        
        
        
        
        
        
    }];

    
    
}

- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block TravelPhotoPageViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.myCollectionView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.next_cursor +=12;
        
        NSLog(@"上拉加载更多，开始网络请求更多数据");
        
        
        // 用AFN加载更多的数据
        [vc getAFNData:vc.next_cursor];
        
        vc.isUpLoading = YES;
        
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
